
%MACRO plot_cll_diffs(in);

****************************************************************************;
****************************************************************************;
********   Print Plots of Differences by CLL Surival Functions   ***********;
****************************************************************************;
****************************************************************************;

options PAPERSIZE=LETTER
	orientation=portrait
	topmargin="0.00in"
	bottommargin="0.00in"
	leftmargin="0.00in"
	rightmargin="0.00in"
	nodate nonumber;

options nodate nonumber;
ods noproctitle;
ODS ESCAPECHAR='^'; 
%let date = %sysfunc(date(),DATE7.);

data loc.diffs;
	set loc.&in;
	y = -y; * Change from log(H(VTC)) - log(H(CDDC))
			* to log(H(CDDC)) - log(H(VTC));
run;

%MACRO master_diff_curves(var,type_list,data);

%local i;
%let i = 1;
%let vals = a b c d e f;

%if &var =opi %then %let text  = 'Figure S10$. Difference between the log of the weighted, adjusted cumulative hazard of opiate relapse for each group.';
%else %if &var =amp %then %let text  =  'Figure S11$. Difference between the log of weighted, adjusted cumulative hazard of amphetamine relapse for each group.';
%else %if &var =bzd %then %let text = 'Figure S. Difference between the log of weighted, adjusted cumulative hazard of benzodiazepine relapse for each group.';
%else %if &var =any %then %let text  = 'Figure S12$. Difference between the log of weighted, adjusted cumulative hazard of illicit-drug relapse for each group.';
%else %if &var =mtd %then %let text  = 'Figure S13$. Difference between the log of weighted, adjusted cumulative hazard of methadone linkage for each group.';
%else %if &var =bup %then %let text  = 'Figure S. Difference between the log of weighted, adjusted cumulative hazard of buprenorphine use for each group.';
%else %if &var =hiv %then %let text  =  'Figure S. Difference between the log of weighted, adjusted cumulative hazard of HIV infection for each group.';

%if &var = opi %then %let lab  = Difference in complementary log-log of the probability of no opiate use;
%else %if &var =amp %then %let lab  = Difference in complementary log-log of the probability of no amphetamine use;
%else %if &var =bzd %then %let lab = Difference in complementary log-log of the probability of no benzodiazepine use;
%else %if &var =any %then %let lab  = Difference in complementary log-log of the probability of no illicit-drug use;	
%else %if &var =mtd %then %let lab  = Difference in complementary log-log of the probability of no methadone linkage;
%else %if &var =bup %then %let lab  = Difference in complementary log-log of the probability of no buprenorphine use;
%else %if &var =hiv %then %let lab  =  Difference in complementary log-log of the probability of no HIV infection;

%do %while("%scan(&type_list,&i)" NE "");

	%let type = %scan(&type_list, &i);
	%let val = %scan(&vals, &i);
	%let title1 = %sysfunc(translate(&text,&val,$));

	%if &type =A %then %let text2 = (Missing measurements are ignored);
	%else %if &type =B %then %let text2 = (Missing measurements coded as an event);
	%else %if &type =C %then %let text2 = (Missing measurements coded as a non-event);
	%else %if &type =D %then %let text2 = D- Worst case scenario;
	%else %if &type =E %then %let text2 = E- Missing equivalent to censored;
	%else %if &type =F %then %let text2 = F- Intervening missing a non-event/trailing missing an event;
	%else %if &type =G %then %let text2 = (Reverse event coding where missing is ignored);
	%else %if &type =H %then %let text2 = (Missing measurements coded with differential risk of event);

	title &title1;
	title2 "&text2";

	proc sgpanel data=loc.&data NOAUTOLEGEND;
		where type = "&type" and var = "&var";
		panelby combin /COLUMNS=3 NOVARNAME;
		*scatter y= y x= x ;
		LOESS y= y x= x /DEGREE=2 NOMARKERS;
		ROWAXIS label = "&lab";
		COLAXIS label ="Time from release/discharge (days)";
		format combin combin2_.;
	run; 
	quit;

	title;
	title2;

	%let i = %eval(&i + 1);

%end;

%let text = 
Note: For each stage 1 imputation combination, 
plots displaying
the log of the cumulative hazard of the CDDC group minus the 
the log of the cumulative hazard of the VTC group.
The values were obtained by first
generating Cox regression models stratified by group, 
weighted by the inverse propensity of seeking treatment, 
and adjusted for measurements of opiate cravings 
and ambivalence, recognition, and taking steps towards change. 
After averaging over the stage 2 imputation results (M=5),
predicted values of local regression for each group were differenced over
values spanning the first year post-release/discharge. 
The displayed are the local regression smoothing line of this difference.
The heading for each subplot reads CDDC imputation model + 
VTC imputation model. Each set of plots uses a 
separate event definition as described in the subtitle.^2n;

ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}&text";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}VTC: Voluntary Treatment Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}CDDC: Compulsory Drug Detention Center";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SR: Stochastic regression imputation";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV90: Single value imputation of length of inpatient stay (90 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV30: Single value imputation of length of inpatient stay (30 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV14: Single value imputation of time of first outcome measurement (14 days)";
ods text = "^S={LEFTMARGIN = 1in RIGHTMARGIN = 1in}SV0: Single value imputation of time of first outcome measurement (0 days)";

%MEND;


%let file = "&out_dir.\CDDC Urine Supp Cox CLL CumHaz Diffs &date..rtf";
ods rtf file = &file Style = custom_narrow startpage = Yes BODYTITLE;

%let data = diffs;
%let var = opi;
%let type_list = A B C H;

%master_diff_curves(&var,&type_list,&data);

%let data = diffs;
%let var = amp;
%let type_list = A B C H;

%master_diff_curves(&var,&type_list,&data);

%let data = diffs;
%let var = any;
%let type_list = A B C H;

%master_diff_curves(&var,&type_list,&data);


%let data = diffs;
%let var = mtd;
%let type_list = A B C;

%master_diff_curves(&var,&type_list,&data);

ods rtf close;

****************************************************************************;
****************************************************************************;
*********************   Remove old datasets               ******************;
****************************************************************************;
****************************************************************************;

proc datasets lib=loc nolist;       
	delete diffs;   
run;
quit;

****************************************************************************;
****************************************************************************;
*****************                   End                        *************;
****************************************************************************;
****************************************************************************;

%MEND plot_cll_diffs;
